﻿using System;
using System.Web;
using System.Xml;

namespace SZJT.Framework.Adapter
{
    class PublicServiceAdapter:IAdapter
    {
        private Type _type;
        private string Uid { get; set; }
        private string Protocol { get; set; }
        private object ServiceInstance { get; set; }
        private string PublicKey { get; set; }
        private string TaskList { get; set; }
        public string Category
        {
            get { return Constants.PUBLIC_ADAPTER; }
        }
        public string UserId
        {
            get { return Uid; }
            set { Uid = value; }
        }
        public string PublicKeyString
        {
            get { return PublicKey; }
        }
        public string TaskListString
        {
            get { return TaskList; }
        }

        /// <summary>
        /// 单项数据读取方法
        /// </summary>
        /// <param name="taskGuid">应用标识</param>
        /// <param name="dataGuid">数据标识</param>
        /// <param name="dataType">数据类型</param>
        /// <returns>xmlDocument 查询结果</returns>
        public XmlNode GetData(string taskGuid, string dataGuid, string dataType)
        {
            if (string.IsNullOrEmpty(taskGuid) || string.IsNullOrEmpty(dataGuid) || string.IsNullOrEmpty(dataType))
            {
                LoggerHelper.WriteCodePointInfo("输入参数有空值!");
                return null;
            }
            //if (string.IsNullOrEmpty(Uid))
            //{
            //    LoggerHelper.WriteCodePointInfo("未能获取调用组件账号");
            //    return null;
            //}
            if (string.IsNullOrEmpty(Protocol))
            {
                LoggerHelper.WriteCodePointInfo("适配器未初始化");
                return null;
            }
            if (null == ServiceInstance)
                ServiceInstance = WebServiceHelper.CallbackWebService(Protocol, out _type);

            if (null == ServiceInstance)
            {
                LoggerHelper.WriteCodePointInfo("创建Webservices代理类失败");
                return null;
            }
            var methodInstance = _type.GetMethod(Constants.GET_METHOD_NAME);
            var serviceResult = methodInstance.Invoke(ServiceInstance, new object[] { Uid, taskGuid, dataGuid, dataType });
            if (null == serviceResult || string.IsNullOrEmpty(serviceResult.ToString()))
            {
                LoggerHelper.WriteCodePointInfo("调用Webservices代理类失败");
                return null;
            }
            var result = new XmlDocument();
            result.LoadXml(serviceResult.ToString());
            return result;
        }
        /// <summary>
        /// 数据写入方法
        /// </summary>
        /// <param name="taskGuid">应用标识</param>
        /// <param name="dataGuid">数据标识</param>
        /// <param name="dataType">数据类型</param>
        /// <param name="xmlData">写入字符串</param>
        /// <returns>int 写入结果</returns>
        public int SetData(string taskGuid, string dataGuid, string dataType, XmlNode xmlData)
        {
            if (string.IsNullOrEmpty(taskGuid) || string.IsNullOrEmpty(dataGuid) || string.IsNullOrEmpty(dataType) || null == xmlData)
            {
                LoggerHelper.WriteCodePointInfo("输入参数有空值");
                return -99;
            }
            //if (null == HttpContext.Current)
            //{
            //    LoggerHelper.WriteCodePointInfo("HttpContext初始化失败");
            //    return -99;
            //}
            if (taskGuid.Equals(Constants.ORG_GUID) && dataGuid.Equals(Constants.INIT_GUID) && dataType.Equals(Constants.INIT_TYPE) && string.IsNullOrEmpty(Uid))
            {
                return InitAdapter(xmlData);
            }
            if (string.IsNullOrEmpty(Protocol))
            {
                LoggerHelper.WriteCodePointInfo("代理类初始化失败");
                return -99;
            }
            if (null == ServiceInstance)
                ServiceInstance =
                    WebServiceHelper.CallbackWebService(Protocol, out _type);
            if (null == ServiceInstance)
            {
                LoggerHelper.WriteCodePointInfo("创建Webservices代理类失败");
                return -99;
            }
            var methodInstance = _type.GetMethod(Constants.SET_METHOD_NAME);
            var serviceResult = methodInstance.Invoke(ServiceInstance, new object[] { Uid, taskGuid, dataGuid, dataType, xmlData.OuterXml });
            if (null == serviceResult)
            {
                LoggerHelper.WriteCodePointInfo("调用Webservices代理类失败");
                return -99;
            }
            return (Convert.ToInt32(serviceResult));
        }
        /// <summary>
        /// 初始化适配器
        /// </summary>
        /// <param name="xmlData">初始化配置信息</param>
        /// <returns>int 初始化结果</returns>
        public int InitAdapter(XmlNode xmlData)
        {
            var userIdNode = xmlData["UserID"];
            var protocol = xmlData["Protocol"];
            var taskList = xmlData["TaskList"];
            if (null == protocol)
                return -99;
            if (null == userIdNode)
                return -99;
            if (null == taskList)
                return -99;
            Protocol = protocol.InnerXml;
            //Uid = userIdNode.InnerXml;
            TaskList = taskList.InnerXml;
            return 0;
        }
        /// <summary>
        /// 复杂查询方法
        /// </summary>
        /// <param name="taskGuid">应用标识</param>
        /// <param name="xmlTransform">查询输入字符串</param>
        /// <returns>xmlDoucument查询结果</returns>
        public XmlNode TransformData(string taskGuid, XmlNode xmlTransform)
        {
            if (string.IsNullOrEmpty(taskGuid) || null == xmlTransform)
            {
                LoggerHelper.WriteCodePointInfo("输入参数有空值!");
                return null;
            }
            if (string.IsNullOrEmpty(Uid))
            {
                LoggerHelper.WriteCodePointInfo("未能获取调用组件账号");
                return null;
            }
            if (string.IsNullOrEmpty(Protocol))
            {
                LoggerHelper.WriteCodePointInfo("适配器初始化失败");
                return null;
            }
            if (null == ServiceInstance)
                ServiceInstance =
                    WebServiceHelper.CallbackWebService(Protocol, out _type);
            if (null == ServiceInstance)
            {
                LoggerHelper.WriteCodePointInfo("创建Webservices代理类失败");
                return null;
            }
            var methodInstance = _type.GetMethod(Constants.TRANSFORM_DATA);
            var serviceResult = methodInstance.Invoke(ServiceInstance, new object[] { Uid, taskGuid, xmlTransform.OuterXml });
            if (null == serviceResult)
            {
                LoggerHelper.WriteCodePointInfo("调用Webservices代理类失败");
                return null;
            }
            var result = new XmlDocument();
            result.LoadXml(serviceResult.ToString());
            return result;
        }
    }
}
